package com.ocom.emqx.timer;

import com.alibaba.fastjson.JSON;
import com.ocom.common.enums.OpTypeDiffEnum;
import com.ocom.common.request.payhandle.QrCodeQueryRequest;
import com.ocom.common.resp.Result;
import com.ocom.emqx.client.PayoperFeignService;
import com.ocom.emqx.handle.MqttSendClient;
import com.ocom.emqx.vo.QrCodePayingVo;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class UserRunnableTask implements Runnable {

    private QrCodePayingVo param;

    private RedisUtil redisUtil;

    private MqttSendClient mqttSendClient;

    private PayoperFeignService payoperFeignService;

    public Logger log ;

    public UserRunnableTask(QrCodePayingVo param, RedisUtil redisUtil, MqttSendClient mqttSendClient, PayoperFeignService payoperFeignService,Logger logger) {
        this.param = param;
        this.redisUtil = redisUtil;
        this.mqttSendClient = mqttSendClient;
        this.payoperFeignService = payoperFeignService;
        this.log = logger;
    }

    @Override
    public void run() {

        try {
            QrCodeQueryRequest request = new QrCodeQueryRequest();
            request.setMachNo(param.getMachNo());
            request.setCompanyId(param.getCompanyId().longValue());
            request.setTradeNo(param.getTradeNo());
            Result result = payoperFeignService.qrCodeQuery(request);
            log.info("请求方式:【{}】,机器号:【{}】【{}】,请求参数:【{}】,返回:【{}】", "定时任务订单查询2", param.getMachNo(),param.getTradeNo(), request, result);
            if (result.getCode() == 200) {
                redisUtil.zRemove("mqtt:orderPaying", param);
                boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + param.getTradeNo(), 100);
                if (!lock2) {
                    return;
                }
                Map<String, Object> map = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);
                String mes = "收款";

                if (map.get("opTypeDiff") != null) {
                    Integer type = (Integer) map.get("opTypeDiff");
                    String payName = OpTypeDiffEnum.getName(type);
                    if (payName != null) mes = payName + mes;
                }

                mqttSendClient.publish(false, param.getMachNo() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + param.getMoney() + "\",\"msg\":\"" + mes + param.getMoney() + "元" + "\"}");
            } else if (result.getCode() == 3030) {
                redisUtil.zRemove("mqtt:orderPaying", param);
                boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + param.getTradeNo(), 100);
                if (!lock2) {
                    return;
                }
                mqttSendClient.publish(false, param.getMachNo() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + param.getMoney() + "\",\"msg\":\"" + result.getMsg() + "\"}");
                log.info("请求方式:【{}】,机器号:【{}】,请求参数:【{}】,返回:【{}】", "定时任务订单查询", param.getMachNo(), request, result);
            } else {
                //继续查询
                redisUtil.set("mqtt:orderSearch:" + param.getTradeNo(), 0, 70l);
//                mqttSendClient.publish(false,param.getMachNo()+"/submsg","{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"定时任务查询中\"}");
            }
//            System.out.println("redis:"+search);
//            System.out.println("参数 " + param);
        } catch (Exception exception) {
            log.info("定时任务查询异常:【{}】,机器号:【{}】,请求参数:【{}】,返回:【{}】", "定时任务订单查询", param.getMachNo(), param, exception);
            //开始查询
            redisUtil.set("mqtt:orderSearch:" + param.getTradeNo(), 0, 70l);
            //    mqttSendClient.publish(false, param.getMachNo() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"定时任务异常\"}");
        }

    }
}
